local skill = fk.CreateSkill{
  name = "tea__kouhuo",
}
---@type TrigSkelSpec<TrigFunc>
local kouhuo = {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skill.name) and data.extra_data then
      return data.extra_data.tea__kouhuo_slash
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, skill.name, "drawcard")
    if data.extra_data.tea__kouhuo_damage ~= player.id then
      player:broadcastSkillInvoke(skill.name, 2)
      player:drawCards(1, skill.name)
    end
    if data.damageDealt then
      player:broadcastSkillInvoke(skill.name, 3)
      target:drawCards(1, skill.name)
    end
  end,
}
---@type TrigSkelSpec<TrigFunc>
local kouhuo_re = {
  can_refresh = function(self, event, target, player, data)
    if event == fk.EventAcquireSkill or event == fk.EventLoseSkill then
      return data == Fk.skills[skill.name]
    elseif event == fk.BuryVictim then
      return target:hasSkill(skill.name, true, true)
    elseif event == fk.AfterPropertyChange then
      return target == player
    elseif event == fk.Damaged then
      if target == player and player:hasSkill(skill.name, true) then
        local room = player.room
        local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn)
        local events = {}
        if turn_event then
          room.logic:getEventsByRule(GameEvent.UseCard, 999, function (e)
            local use = e.data
            if use.card == data.card and (use.extra_data or {}).tea__kouhuo_slash then
              table.insert(events, use)
            end
          end, turn_event.id)
        end
        if #events > 0 then
          local use = events[1]
          event:setCostData(self, use)
          return (use.extra_data or {}).tea__kouhuo_slash
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.Damaged then
      local extra_data = event:getCostData(self) or {}
      extra_data.tea__kouhuo_damage = player.id
    else
      local attached = table.find(room.alive_players, function (p)
        return p:hasSkill(skill.name)
      end)
      if attached and not player:hasSkill("tea__kouhuo&", true, true) then
        room:handleAddLoseSkills(player, "tea__kouhuo&", nil, false, true)
      elseif not attached and player:hasSkill("tea__kouhuo&", true, true) then
        room:handleAddLoseSkills(player, "-tea__kouhuo&", nil, false, true)
      end
    end
  end,
}
skill:addEffect(fk.CardUseFinished, kouhuo)
skill:addEffect(fk.EventAcquireSkill, kouhuo_re)
skill:addEffect(fk.EventLoseSkill, kouhuo_re)
skill:addEffect(fk.BuryVictim, kouhuo_re)
skill:addEffect(fk.AfterPropertyChange, kouhuo_re)
skill:addEffect(fk.Damaged, kouhuo_re)
return skill